package hsl.study.controller;


import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;


/**
 * @Description:
 * @Author: hsl
 * @CreateDate: 2025/7/31 15:39
 * @Version: 1.0
 */

@RestController
@RequestMapping("/prompt")
public class PromptController {

    // v1 通过ChatModel实现stream流的形式进行结果输出
    @Resource(name = "deepseek")
    private OpenAiChatModel deepSeek;
    @Resource(name = "qwenPlus")
    private OpenAiChatModel qwenPlus;

    // v2 通过ChatClient实现stream流的形式进行结果输出
    @Resource(name = "deepseekChatClient")
    private ChatClient deepSeekClient;
    @Resource(name = "qwenPlusChatClient")
    private ChatClient qwenClient;

    @GetMapping("/chat1")
    public Flux<String> chat1(String question) {
        return deepSeekClient.prompt()
                .system("你是一个专门处理法律知识的AI助手，只回答法律问题，对于其他问题的答复:我是法律AI助手，只能解决法律相关问题，其余问题暂无解答")
                .user(question)
                .stream()
                .content();
    }

    @GetMapping("/chat2")
    public Flux<ChatResponse> chat2(String question) {
         // 系统消息
        SystemMessage systemMessage = new SystemMessage("你是一个讲故事的AI智能助手，每个故事控制在300字以内");
        // 用户消息
        UserMessage userMessage = new UserMessage(question);

        Prompt prompt = new Prompt(userMessage,systemMessage);
        return deepSeek.stream(prompt);
    }

    @GetMapping("/chat3")
    public Flux<String> chat3(String question) {
        // 系统消息
        SystemMessage systemMessage = new SystemMessage("你是一个讲故事的AI智能助手，每个故事控制在300字以内");
        // 用户消息
        UserMessage userMessage = new UserMessage(question);

        Prompt prompt = new Prompt(systemMessage,userMessage);
        return deepSeek.stream(prompt)
                .map(chatResponse -> chatResponse.getResults().get(0).getOutput().getText());
    }
}
